﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BinhSon.Dao;

namespace BinhSon.Services.Impl
{
    public partial class BinhSonServices
    {
        #region public methods
        public void ThemGiaoVien(GiaoVien gv)
        {
            this._bsEntities.GiaoVien.Add(gv);
            this._bsEntities.SaveChanges();
        }

        public IList<GiaoVien> GetListOfGiaoVienChuaChuNhiem()
        {
            return this._bsEntities.GiaoVien.Where(x => !x.LopChuNhiemId.HasValue).ToList();
        }

        public IList<int> GetListOfLopHocCoChuNhiem()
        {
            return this._bsEntities.GiaoVien.Where(x => x.LopChuNhiemId.HasValue).Select(x => x.LopChuNhiemId.Value).ToList();
        }

        public void CapNhatLopChuNhiem(int giaoVienId, int lopChuNhiemId)
        {
            var gv = this._bsEntities.GiaoVien.SingleOrDefault(x => x.GiaoVienId == giaoVienId);
            if (gv == null) return;
            gv.LopChuNhiemId = lopChuNhiemId;
            this._bsEntities.SaveChanges();
        }

        public bool ImportGiaoVienFromDataSet(DataSet ds)
        {
            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow row in table.Rows)
                {
                    var itemArray = row.ItemArray;
                    var entity = new GiaoVien
                    {
                        HoVaChuLot = itemArray[0].ToString(),
                        Ten = itemArray[1].ToString(),
                        NgaySinh = (DateTime)itemArray[3],
                        NoiSinh = itemArray[4].ToString(),
                        Email = itemArray[5].ToString(),
                    };
                    entity.TenDayDu = entity.HoVaChuLot + " " + entity.Ten;
                    entity.GioiTinh = itemArray[2].ToString().ToLower() == "nam";
                    this._bsEntities.GiaoVien.Add(entity);
                    this._bsEntities.SaveChanges();
                }
            }
            return true;
        }

        public void DeleteAllGiaoVien()
        {
            var result = this._bsEntities.GiaoVien.ToList();
            foreach (var giaoVien in result)
            {
                this._bsEntities.GiaoVien.Remove(giaoVien);
            }
            this._bsEntities.SaveChanges();
        }
        #endregion
    }
}
